package router

import (
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/files"
	"github.com/swaggo/gin-swagger"
	"log"
	"rock/customer/controller/master"
	"rock/customer/docs"
	"rock/customer/initParam"
	"rock/customer/middleware"
	"rock/customer/public"
	"rock/customer/service/master"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8080
// @BasePath /api/v1
// @query.collection.format multi

// @securityDefinitions.basic BasicAuth

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization

// @securitydefinitions.oauth2.application OAuth2Application
// @tokenUrl https://example.com/oauth/token
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.implicit OAuth2Implicit
// @authorizationurl https://example.com/oauth/authorize
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.password OAuth2Password
// @tokenUrl https://example.com/oauth/token
// @scope.read Grants read access
// @scope.write Grants write access
// @scope.admin Grants read and write access to administrative information

// @securitydefinitions.oauth2.accessCode OAuth2AccessCode
// @tokenUrl https://example.com/oauth/token
// @authorizationurl https://example.com/oauth/authorize
// @scope.admin Grants read and write access to administrative information

// @x-extension-openapi {"example": "value on a json format"}

func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {

	// 初始化 DB
	if err := service.InitDefaultGormDB(); err != nil {
		log.Fatalf("init defaultdb failed %v", err)
	}
	controller.InitDB()

	// 初始化数据库部分数据
	service.InitData()

	// 雪花算法id
	if err := public.Init("2021-02-21", 1); err != nil {
		log.Fatalf(" Init snowflake failed err:%v\n", err)
	}

	// Swagger文档
	docs.SwaggerInfo.Title = initParam.SwaggerConf.Title
	docs.SwaggerInfo.Description = initParam.SwaggerConf.Desc
	docs.SwaggerInfo.Version = "1.0.0"
	docs.SwaggerInfo.Host = initParam.SwaggerConf.Host
	docs.SwaggerInfo.BasePath = initParam.SwaggerConf.BasePath
	docs.SwaggerInfo.Schemes = []string{"http", "https"}

	// 启动引擎
	router := gin.Default()

	// pprof服务
	pprof.Register(router)

	// 错误处理
	router.Use(middleware.Panic)

	// 跨域设置
	router.Use(middleware.Cors())

	// https服务
	//_ = http.ListenAndServeTLS(":443", "cert/www.domain.cn.crt", "cert/www.domain.cn.key", router)

	// ping pong 测试
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	// Swagger路由
	router.GET("/customer/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 业务接口路由
	apiNormalGroup := router.Group("/customer")
	apiNormalGroup.Use(
		// 日志设置
		middleware.RequestLog(),
		// 传输设置
		middleware.TranslationMiddleware(),
	)
	{
		controller.RegisterAuthenticationRouter(apiNormalGroup)
		controller.RegisterRongCloudRouter(apiNormalGroup)
		controller.RegisterGroupRouter(apiNormalGroup)
		controller.RegisterRobotRouter(apiNormalGroup)
		controller.RegisterWkbRouter(apiNormalGroup)
		controller.RegisterChatMesssageRouter(apiNormalGroup)
		controller.RegisterAddrBookRouter(apiNormalGroup)
		controller.RegisterPersonalCenterControllerRouter(apiNormalGroup)
		controller.RegisterTest(apiNormalGroup)
	}

	return router
}
